ryAdmin.service('error', function(dom){
    //错误处理
    this.do_error   = function(info){
        switch(info.code){
            case 102:
                window.open('./login.html', '_self');
            break;
	    case 202:
		window.open('./login.html', '_self');
	    break;
            default:
                dom.do_alert(info.data);
            break;
        }
    } 
});

ryAdmin.service('dom', function($location, $timeout){
    //获取多选参数
    this.get_select_param     = function(paramName){
        var param = '';
        $('[name = "'+paramName+'"').each(function(index){
            if($(this).prop('checked')){
                param   += '&' + paramName + '=' + $(this).val();
            }
        });
        
        return param;
    }

    //启动全选按钮
    this.start_all_check    = function(){
        $timeout(function(){
            $('.check-all').click(function(){
                $(this).closest('.wrap-checkbox')
                    .find('input[type="checkbox"]')
                    .prop('checked', $(this).prop('checked'));
            });
        }, config.dom_delay);
    }

    //提示信息
    this.do_alert   = function(msg) {
        alert(msg);
    }

    //启动分页
    this.start_page    = function(callback){
        $timeout(function(){
            $('.box-page').find('.num').click(function(){
                var page    = $(this).html();
                callback(page);
            });
            //上一页
            $('.box-page').find('.prev').click(function(){
                var page = $('.box-page').find('.active').children('span').html();
                page    = parseInt(page) - 1;
                callback(page);
            });
            //下一页
            $('.box-page').find('.next').click(function(){
                var page = $('.box-page').find('.active').children('span').html();
                page    = parseInt(page) + 1;
                callback(page);
            })
        },config.dom_delay);
    }

    //打开链接
    this.open_url   = function(url){
        url     = url.replace('?', '/');
        url     = url.replace('=', '/');
        $location.path(url);
    }
})

//自定义服务创建
ryAdmin.service('usual', function($rootScope, $http, $location, $timeout, error) {
    //加载菜单
    this.load_menus  = function(){
        var url     = config.prefix_url+'/Menu/getList';
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code == 1){
                $rootScope.menus    = info.data.menus;

                $timeout(function(){
                    //导航展开效果
                    $('.s-list-group').on('show.bs.collapse', function () {
                        $(this).prev('.list-group-item').addClass('active');

                        $(this).prev('.list-group-item').find('.icon-sys')
                            .removeClass('fa-angle-right')
                            .addClass('fa-angle-down');
                    }).on('hide.bs.collapse', function(){
                        $(this).prev('.list-group-item').removeClass('active');

                        $(this).prev('.list-group-item').find('.icon-sys')
                            .removeClass('fa-angle-down')
                            .addClass('fa-angle-right');
                    });
                }, config.dom_delay);
            }else{
                error.do_error(info);
            }
        });
    }
});
